from datetime import datetime, date, timedelta

from rest_framework.generics import ListAPIView
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from apps.goods.models import GoodsVisitCount, SPUSpecification, SPU
from apps.orders.models import OrderInfo
from apps.statistical.serializers import GoodSerializer
from apps.users.models import User
from django.utils import timezone

from rest_framework.views import APIView
def today():
    # 获取当天日期
    now = timezone.now()
    now_date = now - timedelta(hours=23, minutes=59, seconds=59)
    return now_date

# 统计用户总数
class TotalCountView(APIView):
    # 设置管理员权限
    permission_classes = [IsAdminUser]
    def get(self, request):
        count = User.objects.all().count()
        # 获取普通用户
        # count = User.objects.filter(is_staff=False).count()
        # 年月日 十分秒
        # date = datetime.today()
        # 年月日
        now_date = today()
        return Response({"count": count, "date": now_date})


# 统计日增用户

class DayIncrementView(APIView):
    # 指定管理员权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 得到的为现在的年月日  十分秒都为0
        now_date = today()
        # 获取当日注册用户数量 date_joined 记录创建账户时间的字段
        # __gte  大于等于
        count = User.objects.filter(date_joined__gte=now_date, is_staff=False).count()
        return Response({"count": count, "date": now_date})


# 日活跃用户统计

class DayActiveView(APIView):
    # 指定管理员权限
    permission_classes = [IsAdminUser]

    def get(self, requset):
        now_date = today()
        # 获取当日登录用户数量  last_login记录最后登录时间
        count = User.objects.filter(last_login__gte=now_date).count()
        return Response({"count": count, "date": now_date})


# 日下单用户统计

class DayOrdersView(APIView):
    # 指定管理员权限
    permission_classes = [IsAdminUser]

    def get(self, requset):
        now_date = today()
        # 获取当日下单用户数量  orders__create_time 订单创建时间
        count = User.objects.filter(orders__create_time__gte=now_date).distinct().count()
        # select distinct u.* from tb_user as u inner tb_order_info as o on u.id=o.user_id where o.create_time>="2019-10-13"
        # distinct(字段)取除重复
        # count = OrderInfo.objects.filter(create_time__gte=now_date).count()
        return Response({"count": count, "date": now_date})


# 月增用户统计

class MonthIncrementView(APIView):
    # 指定管理员权限
    permission_classes = [IsAdminUser]

    def get(self, requset):
        now_date = today()
        # 获取一个月前日期
        start_date = now_date - timedelta(29)
        # 创建空列表保存每天的用户量
        date_list = []
        for i in range(30):
            # 循环遍历获取当天日期
            index_date = start_date + timedelta(days=i)
            # 指定下一天日期
            cur_date = start_date + timedelta(days=i + 1)
            # 查询条件是大于当前日期index_date，小于明天日期的用户cur_date，得到当天用户量
            count = User.objects.filter(date_joined__gte=index_date, date_joined__lt=cur_date).count()

            date_list.append({
                'count': count,
                'date': index_date
            })

        return Response(date_list)


# 日分类商品访问量

# class GoodsDayViews(APIView):
#     def get(self, request):
#         # 获取当天日期
#         # now_date = timezone.now().strftime('%Y-%m-%d')
#         now_date = date.today()
#         # 获取当天访问的商品分类数量信息
#         data = GoodsVisitCount.objects.filter(date=now_date)
#         # 序列化返回分类数量
#         ser = GoodSerializer(data, many=True)
#         return Response(ser.data)
class GoodsDayViews(ListAPIView):
    now_date = today()
    queryset = GoodsVisitCount.objects.filter(date=now_date).order_by("id")
    serializer_class = GoodSerializer


